package com.zy.qudadid.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.rey.material.widget.Button;
import com.rey.material.widget.CheckBox;
import com.zy.lcpassenger.R;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.RegisterCCheckPresenter;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.ui.view.RegisterCCheckView;
import com.zy.qudadid.utils.ActivityUtil;
import com.zy.qudadid.utils.Bun;
import com.zy.qudadid.utils.StringUtil;

import butterknife.Bind;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by ninos on 2016/7/30.
 */
public class RegisterCCheckActivity extends ToolBarActivity<RegisterCCheckPresenter> implements RegisterCCheckView {

    //常量
//    private static final String APPKEY = "1bc5e299a3089";
//    private static final String APPSECRET = "a9fcb6ea73828e9d1da660e743a0c778";
    private static final int TIME_INIT = 60;        //初始化时间用
    private String phoneNumber;
    private String code;
    EventHandler eh;
    private int time;
    private Handler handler = new Handler();
    private boolean canClick = false;
    private String id;
    private String lng="";
    private String lat="";

    @Bind(R.id.l_login)
    Button l_login;
    @Bind(R.id.cc_resend)
    Button cc_resend;
    @Bind(R.id.l_problem)
    TextView l_problem;
    @Bind(R.id.l_userphone)
    TextView l_userphone;            //手机号
    @Bind(R.id.l_userpwd)
    TextView l_userpwd;              //手机号
    @Bind(R.id.l_check)
    CheckBox l_check;

    @OnClick(R.id.cc_resend)
    public void cc_resend() {
        if (l_userphone.getText().toString().trim().equals("")) {
            snb("手机号不能为空", l_login);
            return;
        }
        if (!StringUtil.isMobile(l_userphone.getText().toString().trim())) {
            snb("手机号格式不正确", l_login);
            return;
        }
        phoneNumber = l_userphone.getText().toString().trim();
        if (cc_resend.getText().toString().equals("验证码"))
            sendMessageCode();
    }

    @OnClick(R.id.l_problem)
    public void lpOnClick(){
        startActivity(OwnWebActivity.class);
    }

    @OnClick(R.id.l_login)
    public void l_login() {
        phoneNumber = l_userphone.getText().toString().trim();
        code = l_userpwd.getText().toString();
        if (l_check.isChecked()){
        }else{
            toast("请同意打车协议后再进行下一步");
            return;
        }
        if (!StringUtil.isEmpty(code)) {
            ActivityUtil.addActivity(RegisterCCheckActivity.this);
            startActivity(RegisterCPwdActivity.class, new Bun().putString("phoneNumber", phoneNumber).putString("lat",lat).putString("lng",lng).ok());
//            SMSSDK.submitVerificationCode("86", phoneNumber, code);
        } else
            snb("验证码不能为空", l_login);
    }

    @Override
    protected CharSequence provideTitle() {
        return "注册司机";
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register_c_check;
    }

    @Override
    public void initListeners() {

    }

    @Override
    public RegisterCCheckPresenter createPresenter() {
        return new RegisterCCheckPresenter();
    }

    /**
     * 验证码时间倒计时
     */
    Runnable runnabletime = new Runnable() {
        @Override
        public void run() {
            time--;
            if (time >= 0) {
                cc_resend.setText(time + "秒");
                handler.postDelayed(this, 1000);
            } else {
                cc_resend.setText("验证码");
            }
        }
    };

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
        // 短信验证
        // 初始化短信验证
        SMSSDK.initSDK(this, Const.APPKEY, Const.APPSECRET);
        eh = new EventHandler() {

            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                handlersms.sendMessage(msg);
            }
        };
        SMSSDK.registerEventHandler(eh); // 注册短信回调

        lat = getIntent().getBundleExtra("data").getString("lat");
        lng = getIntent().getBundleExtra("data").getString("lng");
    }

    /**
     * 手机发送验证码
     */
    public void sendMessageCode() {
        cc_resend.setText(TIME_INIT + "秒");
        time = TIME_INIT;
        handler.postDelayed(runnabletime, 1000);
        SMSSDK.getVerificationCode("86", phoneNumber);
    }

    /**
     * 创建可调用ui的handler
     */
    Handler handlersms = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            super.handleMessage(msg);
            int event = msg.arg1;
            int result = msg.arg2;
            Object data = msg.obj;

            if (result == SMSSDK.RESULT_COMPLETE) {
                // 回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    // 提交验证码成功

                    toast("验证码验证通过");
                    ActivityUtil.addActivity(RegisterCCheckActivity.this);
                    startActivity(RegisterCPwdActivity.class, new Bun().putString("phoneNumber", phoneNumber).putString("lat",lat).putString("lng",lng).ok());
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    // 获取验证码成功
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    // 返回支持发送验证码的国家列表
                }
            } else {
                try {
                    snb(event == SMSSDK.EVENT_GET_VERIFICATION_CODE ? "验证过于频繁，请稍后再试" : "验证码错误", l_userpwd);
                } catch (Exception ex) {
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnabletime);
        SMSSDK.unregisterEventHandler(eh);
    }

}
